﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace BusinessManagementStudio.Models.BusinessObject.Services
{
    public class InvoiceNoStatusService : EntityService<InvoiceNoStatus>
    {
        public List<InvoiceNoStatus> GetAllInvoices()
        {
            return (from invoice in this.MongoConnectionHandler.MongoCollection.FindAllAs<InvoiceNoStatus>()
                    orderby invoice.InvoiceNo
                    select invoice).ToList();
        }

        public String GetNextInvoiceNo()
        {
            int counter = 1;

            List<InvoiceNoStatus> InvoiceList = GetAllInvoices();

            if(InvoiceList.Count > 0)
            {
                String currInvoiceNo = InvoiceList[InvoiceList.Count - 1].InvoiceNo;
                String[] splitInvoiceNo = currInvoiceNo.Split('-');
                counter = int.Parse(splitInvoiceNo[0].Substring(4, 3)) + 1;
            }
            String result = DateTime.Today.ToString("yyMM") + counter.ToString("000");

            return result;
        }

        public override void Update(InvoiceNoStatus entity)
        {
            //// Not necessary for the example
        }
    }
}